﻿package core.drawing
{
    import __AS3__.vec.*;
    import core.enum.*;
    import flash.display.*;
    import flash.events.*;
    import flash.filters.*;
    import flash.geom.*;

    final public class SpotHealTool extends ShapeTool
    {
        var soffset:Point;
        var source:BitmapData;
        var n:int;
        var cx:int;
        var cy:int;
        var sx:int;
        var sy:int;
        var ux:int;
        var uy:int;
        var alpha:int;
        var a:int;
        var ta:int;
        var tr:int;
        var tg:int;
        var tb:int;
        var max:int;
        var px:uint;
        var pt:uint;

        public function SpotHealTool(param1:Tuyitu)
        {
            super(param1, ToolType.SpotHeal);
            return;
        }// end function

        override public function MouseDown(event:MouseEvent) : void
        {
            if (!type.attitude.locked)
            {
                var _loc_2:int = -1;
                y = -1;
                x = _loc_2;
                this.UpdateReferences();
                this.Prepare(Action.SpotHealTool, layer.SetBitmapEditMode(0.65, canvas.clip));
                this.source = layer.bitmap.bitmapData;
                this.soffset = new Point(layer.x, layer.y);
                this.tr = 255;
                this.tg = 255;
                this.tb = 255;
                this.MouseDrag(null);
            }
            return;
        }// end function

        override public function DrawShape(SpotHealTool:int, SpotHealTool:int) : void
        {
            this.n = 0;
            this.sy = SpotHealTool - int(shape.height * 0.5);
            this.cy = this.sy + shape.height;
            while (this.sy < this.cy)
            {
                
                this.sx = SpotHealTool - int(shape.width * 0.5);
                this.cx = this.sx + shape.width;
                while (this.sx < this.cx)
                {
                    
                    if (target.rect.contains(this.sx + this.ux, this.sy + this.uy))
                    {
                        var _loc_3:* = shape.data[this.n];
                        this.alpha = shape.data[this.n];
                        this.ta = _loc_3;
                        this.max = selection != null ? (selection.Max(this.sx + this.ux + offset.x, this.sy + this.uy + offset.y)) : (255);
                        if (this.ta > 0)
                        {
                            if (this.ta < this.max)
                            {
                                this.px = target.getPixel32(this.sx + this.ux, this.sy + this.uy);
                                this.a = this.px >>> 24 & 255;
                                if (this.a > 0)
                                {
                                    if (this.a == 255)
                                    {
                                        this.ta = 255;
                                    }
                                    else
                                    {
                                        this.ta = this.ta + (255 - this.alpha) * this.a / 255;
                                    }
                                }
                                if (this.ta > this.max)
                                {
                                    this.ta = this.max;
                                }
                                this.pt = this.ta << 24 | this.tr << 16 | this.tg << 8 | this.tb;
                                if (this.px != this.pt)
                                {
                                    target.setPixel32(this.sx + this.ux, this.sy + this.uy, this.pt);
                                }
                            }
                            else
                            {
                                if (this.ta > this.max)
                                {
                                    this.ta = this.max;
                                }
                                target.setPixel32(this.sx + this.ux, this.sy + this.uy, this.ta << 24 | this.tr << 16 | this.tg << 8 | this.tb);
                            }
                        }
                    }
                    var _loc_3:String = this;
                    var _loc_4:* = this.n + 1;
                    _loc_3.n = _loc_4;
                    var _loc_3:String = this;
                    var _loc_4:* = this.sx + 1;
                    _loc_3.sx = _loc_4;
                }
                var _loc_3:String = this;
                var _loc_4:* = this.sy + 1;
                _loc_3.sy = _loc_4;
            }
            return;
        }// end function

        override public function MouseUp(event:MouseEvent) : void
        {
            var _loc_2:Vector.<uint> = null;
            var _loc_3:Boolean = false;
            var _loc_4:Boolean = false;
            var _loc_5:Boolean = false;
            var _loc_6:Boolean = false;
            var _loc_7:Boolean = false;
            var _loc_8:int = 0;
            var _loc_9:int = 0;
            ed.cursor.freeze = false;
            ed.removeEventListener(MouseEvent.MOUSE_MOVE, MouseDrag, false);
            ed.removeEventListener(MouseEvent.MOUSE_UP, this.MouseUp, false);
            layer.editLayer.alpha = 1;
            var _loc_10:* = new BitmapData(target.width, target.height, false, 0);
            _loc_10.draw(target);
            var _loc_11:* = _loc_10.getColorBoundsRect(16777215, 0, false);
            var _loc_12:* = _loc_11.width < _loc_11.height ? (_loc_11.width / 6) : (_loc_11.height / 6);
            _loc_10.applyFilter(target, target.rect, target.rect.topLeft, new BlurFilter(_loc_12, _loc_12, 3));
            target.fillRect(_loc_10.rect, 16777215);
            _loc_11 = _loc_10.getColorBoundsRect(16777215, 0, false);
            if (_loc_11.width > 0 && _loc_11.height > 0)
            {
                _loc_2 = new Vector.<uint>;
                _loc_8 = _loc_11.top;
                while (_loc_8 <= _loc_11.bottom)
                {
                    
                    _loc_4 = true;
                    _loc_5 = Boolean(_loc_10.getPixel(_loc_11.left, _loc_8) < 4210752);
                    _loc_9 = _loc_11.left;
                    while (_loc_9 <= _loc_11.right)
                    {
                        
                        _loc_3 = _loc_4;
                        _loc_4 = _loc_5;
                        _loc_5 = Boolean(_loc_10.getPixel((_loc_9 + 1), _loc_8) < 4210752);
                        if (!_loc_4)
                        {
                            _loc_6 = Boolean(_loc_10.getPixel(_loc_9, (_loc_8 - 1)) < 4210752);
                            _loc_7 = Boolean(_loc_10.getPixel(_loc_9, (_loc_8 + 1)) < 4210752);
                            if (_loc_3 && _loc_6)
                            {
                                _loc_2.push(this.source.getPixel32(_loc_9 + offset.x - this.soffset.x + 1, _loc_8 + offset.y - this.soffset.y));
                            }
                            else if (_loc_6 && _loc_5)
                            {
                                _loc_2.push(this.source.getPixel32(_loc_9 + offset.x - this.soffset.x - 1, _loc_8 + offset.y - this.soffset.y));
                            }
                            else if (_loc_3 && _loc_7)
                            {
                                _loc_2.push(this.source.getPixel32(_loc_9 + offset.x - this.soffset.x + 1, _loc_8 + offset.y - this.soffset.y));
                            }
                            else if (_loc_7 && _loc_5)
                            {
                                _loc_2.push(this.source.getPixel32(_loc_9 + offset.x - this.soffset.x - 1, _loc_8 + offset.y - this.soffset.y));
                            }
                            else if (_loc_6)
                            {
                                _loc_2.push(this.source.getPixel32(_loc_9 + offset.x - this.soffset.x, _loc_8 + offset.y - this.soffset.y + 1));
                            }
                            else if (_loc_3)
                            {
                                _loc_2.push(this.source.getPixel32(_loc_9 + offset.x - this.soffset.x + 1, _loc_8 + offset.y - this.soffset.y));
                            }
                            else if (_loc_5)
                            {
                                _loc_2.push(this.source.getPixel32(_loc_9 + offset.x - this.soffset.x - 1, _loc_8 + offset.y - this.soffset.y));
                            }
                            else if (_loc_7)
                            {
                                _loc_2.push(this.source.getPixel32(_loc_9 + offset.x - this.soffset.x, _loc_8 + offset.y - this.soffset.y - 1));
                            }
                            if (_loc_3 || _loc_6 || _loc_5 || _loc_7)
                            {
                                _loc_2.push(this.source.getPixel32(_loc_9 + offset.x - this.soffset.x, _loc_8 + offset.y - this.soffset.y));
                            }
                        }
                        _loc_9++;
                    }
                    _loc_8++;
                }
                _loc_8 = _loc_11.top;
                while (_loc_8 <= _loc_11.bottom)
                {
                    
                    _loc_9 = _loc_11.left;
                    while (_loc_9 <= _loc_11.right)
                    {
                        
                        this.a = _loc_10.getPixel(_loc_9, _loc_8) & 255;
                        if (this.a > 0)
                        {
                            this.px = _loc_2[Math.round(Math.random() * (_loc_2.length - 1))];
                            this.tr = this.px >>> 16 & 255;
                            this.tg = this.px >>> 8 & 255;
                            this.tb = this.px & 255;
                            target.setPixel32(_loc_9, _loc_8, this.a << 24 | this.tr << 16 | this.tg << 8 | this.tb);
                        }
                        _loc_9++;
                    }
                    _loc_8++;
                }
            }
            target.applyFilter(target, target.rect, target.rect.topLeft, new BlurFilter(1.2, 1.2, 3));
            layer.RemoveBitmapEditMode();
            canvas.UpdateAfterEdit();
            history.Commit();
            if (!AppState.OnCanvas)
            {
                ed.cursor.Set(ToolType.None);
            }
            return;
        }// end function

    }
}
